package org.dromara.bzIns.service.impl;

import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.dromara.bzIns.domain.bo.InsQuoteBo;
import org.dromara.bzIns.domain.vo.InsQuoteVo;
import org.dromara.bzIns.domain.InsQuote;
import org.dromara.bzIns.mapper.InsQuoteMapper;
import org.dromara.bzIns.service.IInsQuoteService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 保险报价Service业务层处理
 *
 * @author bz_huaYal9146
 * @date 2025-03-28
 */
@RequiredArgsConstructor
@Service
public class InsQuoteServiceImpl implements IInsQuoteService {

    private final InsQuoteMapper baseMapper;

    /**
     * 查询保险报价
     *
     * @param quoteId 主键
     * @return 保险报价
     */
    @Override
    public InsQuoteVo queryById(Long quoteId){
        return baseMapper.selectVoById(quoteId);
    }

    /**
     * 分页查询保险报价列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 保险报价分页列表
     */
    @Override
    public TableDataInfo<InsQuoteVo> queryPageList(InsQuoteBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<InsQuote> lqw = buildQueryWrapper(bo);
        Page<InsQuoteVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的保险报价列表
     *
     * @param bo 查询条件
     * @return 保险报价列表
     */
    @Override
    public List<InsQuoteVo> queryList(InsQuoteBo bo) {
        LambdaQueryWrapper<InsQuote> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<InsQuote> buildQueryWrapper(InsQuoteBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<InsQuote> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(InsQuote::getQuoteId);
        lqw.eq(bo.getUserId() != null, InsQuote::getUserId, bo.getUserId());
        lqw.eq(bo.getCarId() != null, InsQuote::getCarId, bo.getCarId());
        lqw.eq(StringUtils.isNotBlank(bo.getCarPlate()), InsQuote::getCarPlate, bo.getCarPlate());
        lqw.like(StringUtils.isNotBlank(bo.getInsCompanyName()), InsQuote::getInsCompanyName, bo.getInsCompanyName());
        lqw.eq(bo.getIsDisplay() != null, InsQuote::getIsDisplay, bo.getIsDisplay());
        return lqw;
    }

    /**
     * 新增保险报价
     *
     * @param bo 保险报价
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(InsQuoteBo bo) {
        InsQuote add = MapstructUtils.convert(bo, InsQuote.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setQuoteId(add.getQuoteId());
        }
        return flag;
    }

    /**
     * 修改保险报价
     *
     * @param bo 保险报价
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(InsQuoteBo bo) {
        InsQuote update = MapstructUtils.convert(bo, InsQuote.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(InsQuote entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除保险报价信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }
}
